import { LayoutComponentName } from "@/types/enum/enum";
import { EnumLayoutComponentName } from "@/types/enum/system";
import { BasicLayout, BlankLayout } from '@components/BasicComponents/layout/index'
import { Component } from "vue";
import {
    // Root,
    Home,
    Login,
    TopicSet,
    Topic,
    TestPaper,
    UserGroup,
    Unit,
    NotFound,
} from '@/views'
import { AuthRoute } from "@/types/auth/route";

/** 布局组件 key-value */
type LayoutComponent = Record<LayoutComponentName, () => Promise<Component>>;
/** 页面组件 key-value */
type viewComponent = Record<AuthRoute.RouteKey, () => Promise<Component>>;

/**
 * 设置组件设置名称
 * @param asyncComponent 设置组件
 * @param name 组件名称
 */
async function setViewComponentName(asyncComponent: () => Promise<Component>, name: string) {
    console.log("渲染页面前面，我们也进来了吗？");
    if (!asyncComponent) {
        return NotFound
    }
    const component = (await asyncComponent()) as { default: Component }

    Object.assign(component.default, { name })

    return component
}

/**
 * 获取用于布局的Vue文件（懒加载）
 * @param layoutType 布局类型
 */
export function getLayoutComponent(layoutType: LayoutComponentName) {
    const layoutComponent: LayoutComponent = {
        basic: BasicLayout,
        blank: BlankLayout,
    }
    return () => setViewComponentName(layoutComponent[layoutType], EnumLayoutComponentName[layoutType])
}

/**
 * 获取页面的Vue文件（懒加载）
 * @param routeKey 路由key
 */
export function getViewComponent(routeKey: AuthRoute.RouteKey) {
    // debugger
    console.log("兄弟，你进来了没？——getViewComponent", routeKey);
    const viewComponents: viewComponent = {
        root: Home,
        login: Login,
        home: Home,
        'topic-set': TopicSet,
        topic: Topic,
        'test-paper': TestPaper,
        'user-group': UserGroup,
        unit: Unit,
        'not-found': NotFound,
        'not-found-page': NotFound,
    }

    /** 处理undefined信息 */
    // const component: viewComponent = viewComponents[routeKey] ? viewComponents[routeKey] : viewComponents['notFound']

    return () => setViewComponentName(viewComponents[routeKey], routeKey) as Promise<Component>
}